package com.huaxin.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.device.common.MyMetaObjectHandler;
import com.huaxin.device.dto.ResponseCode;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.AddDeviceZoneReqDTO;
import com.huaxin.device.dto.request.UpdateDeviceZoneReqDTO;
import com.huaxin.device.mapper.DeviceZoneMapper;
import com.huaxin.device.mapping.DeviceZoneMapping;
import com.huaxin.device.models.DeviceZone;
import com.huaxin.device.service.DeviceZoneService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DeviceZoneServiceImpl extends ServiceImpl<DeviceZoneMapper, DeviceZone> implements DeviceZoneService {
    private final ApplicationContext applicationContext;
    private final CacheManager cacheManager;
    private final MyMetaObjectHandler myMetaObjectHandler;
    private final DeviceZoneMapping deviceZoneMapping;

    /**
     * 添加设备区域
     *
     * @param in 添加设备区域参数
     * @return
     */
    @Override
    public ResponseResult<String> addDeviceZone(AddDeviceZoneReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数错误");
        }
        if (StringUtils.isBlank(in.getZoneName())) {
            return ResponseResult.parameterError(null, "区域名称不能为空");
        }
        if (StringUtils.isBlank(in.getFamilyId()) && StringUtils.isBlank(in.getOrgId())) {
            return ResponseResult.parameterError(null, "设备必须绑定给某个家庭或某个企业！");
        }
        if (StringUtils.isNotBlank(in.getOrgId()) && StringUtils.isNotBlank(in.getFamilyId())) {
            return ResponseResult.parameterError(null, "设备不能同时绑定给家庭和公司！");
        }
        long count = this.count(new LambdaQueryWrapper<DeviceZone>().eq(DeviceZone::getZoneName, in.getZoneName()));
        if (count > 0) {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "该区域已存在！", null);
        }
        DeviceZone deviceZone = deviceZoneMapping.toEntity(in);
        if (this.save(deviceZone)) {
            return ResponseResult.ok(null);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "添加设备区域实例失败，请稍后再试！", null);
        }
    }

    /**
     * 删除设备区域
     *
     * @param id 设备区域ID
     * @return
     */
    @Override
    public ResponseResult<String> deleteDeviceZone(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "设备区域ID不能为空");
        }
        if (this.removeById(id)) {
            return ResponseResult.ok("删除设备区域成功！");
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "删除设备区域失败，请稍后再试！", null);
        }
    }

    /**
     * 修改设备区域
     *
     * @param in 修改设备区域参数
     * @return
     */
    @Override
    public ResponseResult<String> updateDeviceZone(UpdateDeviceZoneReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数错误");
        }
        if (StringUtils.isBlank(in.getId())) {
            return ResponseResult.parameterError(null, "设备区域ID不能为空");
        }
        if (StringUtils.isBlank(in.getZoneName())) {
            return ResponseResult.parameterError(null, "区域名称不能为空");
        }
        DeviceZone deviceZone = this.getById(in.getId());
        deviceZoneMapping.updateEntity(in, deviceZone);
        if (this.updateById(deviceZone)) {
            return ResponseResult.ok(null);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "修改设备区域失败，请稍后再试！", null);
        }
    }

    /**
     * 查询设备区域
     *
     * @param id 设备区域ID
     * @return
     */
    @Override
    public ResponseResult<DeviceZone> queryDeviceZone(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "设备区域ID不能为空");
        }
        return ResponseResult.ok(this.getById(id));
    }

    /**
     * 查询设备区域列表
     *
     * @param pageNum    页码
     * @param pageSize   每页数量
     * @param customerId 客户ID
     * @return
     */
    @Override
    public ResponseResult<Page<DeviceZone>> queryDeviceZoneList(Integer pageNum, Integer pageSize, String customerId) {
        Page<DeviceZone> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DeviceZone> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(customerId)) {
            queryWrapper.and(queryWrapper1 -> queryWrapper1.eq(DeviceZone::getFamilyId, customerId).or().eq(DeviceZone::getOrgId, customerId));
        }
        return ResponseResult.ok(this.page(page, queryWrapper));
    }
}
